const DB = require('../db/index');
const exception = require('../exception');
const fw = require('../core/framework');

module.exports = {
    login(data, successFn, failFn) {
        let getMerchantInfo = function(dbUser){
            DB.Merchant.search({id:dbUser.merchantId||dbUser.merchant_id}).then((dbMerchants)=>{
                let dbMerchant = fw.getDBObject(dbMerchants);
                dbUser.merchantType = dbMerchant.type;
                dbUser.merchantName = dbMerchant.name;
                if (fw.isFn(successFn)){ successFn({code: 0, data: dbUser});}
            })
        };
        try {
            DB.User.search({mobile: data.mobile}).then((dbUsers) => {
                let dbUser = fw.getDBObject(dbUsers);
                if (fw.isExit(dbUser)) {
                    if(fw.isEqual(dbUser.password,data.password)){
                        getMerchantInfo(dbUser)
                    }else{
                        failFn(exception['402']);
                    }
                } else {
                    DB.ReportUser.search({mobile: data.mobile}).then((dbReportUsers) => {
                        let dbReportUser = fw.getDBObject(dbReportUsers);
                        if (fw.isExit(dbReportUser)) {
                            if(fw.isEqual(dbReportUser.password,data.password)){
                                getMerchantInfo(dbReportUser)
                            }else{
                                failFn(exception['402']);
                            }
                        } else {
                            failFn(exception['401']);
                        }
                    })
                }
            })
        } catch (e) {
            failFn(exception.ex(e));
        }
    },
    create(data, successFn, failFn) {
        this.findByMobile(data.mobile, (dbUser) => {
            failFn(exception['601'])
        }, (e) => {
            if (fw.isExit(e)) {
                failFn(exception.ex(e))
            } else {
                try {
                    DB.User.create(data).then((dbUserRes)=>{
                        successFn({code: 0,data:dbUserRes})
                    });
                } catch (e) {
                    failFn(exception.ex(e))
                }
            }
        });
    },
    update(data, successFn, failFn) {
        this.findById(data.id, (dbUser) => {
            try {
                DB.Device.update(data).then(() => {
                    successFn({code: 0})
                });
            } catch (e) {
                failFn(exception.ex(e))
            }
        }, (e) => {
            if (fw.isExit(e)) {
                failFn(exception.ex(e))
            } else {
                failFn(exception['1202'])
            }
        });
    },
    search(data,successFn,failFn){
        try {
            DB.User.searchAll(data).then((res)=>{
                successFn({code:0,data:res});
            });
        } catch (e) {
            failFn(exception.ex(e))
        }
    },
    delete(data, successFn, failFn) {
        this.findById(data.id, (dbUser) => {
            try {
                DB.User.delete({id:data.id}).then(()=>{
                    successFn({code: 0})
                });
            } catch (e) {
                failFn(exception.ex(e))
            }

        }, (e) => {
            e ? failFn(exception.ex(e)) : failFn(exception['1201'])
        });
    },
    findById(userId,successFn, failFn){
        try {
            DB.User.search({id: userId}).then((dbUsers) => {
                let dbUser = fw.getDBObject(dbUsers);
                if (fw.isExit(dbUser)) {
                    if (successFn) successFn(dbUser);
                } else {
                    if (failFn) failFn();
                }
            })
        } catch (e) {
            if (failFn) failFn(e);
        }
    },
    findByMobile(mobile, successFn, failFn) {
        try {
            DB.User.search({mobile: mobile}).then((dbUsers) => {
                let dbUser = fw.getDBObject(dbUsers);
                if (fw.isExit(dbUser)) {
                    if (successFn) successFn(dbUser);
                } else {
                    if (failFn) failFn();
                }
            })
        } catch (e) {
            if (failFn) failFn(e);
        }
    }
};
